'/
' MOTEP - Massive Online Text Environment Project
' --------------------------------------------------------------------------------
' Shared Code
' 
' Strings.bmx
' 
' (c) Anton Berlin. All Rights Reserved.
'/


'// Some useful string constants (79 characters for the long ones and 30 for the short ones).
const DASH_BAR:string = "-------------------------------------------------------------------------------"
const DOUBLE_DASH_BAR:string = "==============================================================================="
const DASH_BREAK:string = "------------------------------"
const DOUBLE_DASH_BREAK:string = "=============================="



'// String library. You are not supposed to create instances of this type. The reason why these functions are in a
'// type is to avoid name clashes.
type str



'// -----------------------------------------------------------------------------------------------------------------
'// Description:
'// 		Reverse a string.
'// 
'// Parameters:
'//		(string) src: The string to reverse.
'// -----------------------------------------------------------------------------------------------------------------
function reverse:string(src:string)

	local reversedString:string
	
	for local i:int = src.length until 1 step -1
	
		reversedString :+ Mid(src, i, 1)
	
	next
	
	return reversedString

end function

'// -----------------------------------------------------------------------------------------------------------------
'// Description:
'// 		Length of a string (where "" returns 0 and "a" returns 1).
'// 
'// Parameters:
'//		(string) src: The source string.
'// -----------------------------------------------------------------------------------------------------------------
function length:string(src:string)

	return src.length

end function

'// -----------------------------------------------------------------------------------------------------------------
'// Description:
'// 		Return part of a string.
'// 
'// Parameters:
'//		(string) src: The source string.
'//		(int) pos: The starting position (counting from 0).
'//		(int) length: Optional. Default value is -1. -1 means "to the end of the string). Counting from 1 (so
'//			length = 0 would mean that you return nothing).
'// -----------------------------------------------------------------------------------------------------------------
function substring:string(str:string, pos:int, length:int = -1)

	return Mid(str, pos + 1, length)

end function

'// -----------------------------------------------------------------------------------------------------------------
'// Description:
'// 		Strip a string from all space-characters.
'// 
'// Parameters:
'//		(string) src: The source string.
'//		(int) char: Optional. Default value is KEY_SPACE. Ascii code of the character to strip away from the
'//			string. If you don't specify this, it assumes you want to strip out blank spaces (spaces not tabs).
'// -----------------------------------------------------------------------------------------------------------------
function strip:string(src:string, char:int = KEY_SPACE)

	local returnedString:string = null
	local charToString:string = chr(char)
	
	for local i:int = 0 to Len(src)
	
		if (Mid(src, i, 1) <> charToString) ..
			returnedString = returnedString + Mid(src, i, 1)
	
	next
	
	return returnedString

end function

'// -----------------------------------------------------------------------------------------------------------------
'// Description:
'// 		Strip a string from all space-characters and tabs.
'// 
'// Parameters:
'//		(string) src: The source string.
'// -----------------------------------------------------------------------------------------------------------------
function superstrip:string(src:string)

	local returnedString:string = null
	
	for local i:int = 0 to Len(src)
	
		if (Mid(src, i, 1) <> "~t" and Mid(src, i, 1) <> " ") ..
			returnedString = returnedString + Mid(src, i, 1)
	
	next
	
	return returnedString

end function

'// -----------------------------------------------------------------------------------------------------------------
'// Description:
'// 		Extract a word from a string.
'// 
'// Parameters:
'//		(string) src: The source string.
'// -----------------------------------------------------------------------------------------------------------------
function word:string(wordNumber:int, src:string, wordSeparater:int = KEY_SPACE)

	local returnWord:string
	local wordCount:int = 0
	
	local totalWords = words(src, wordSeparater) - 1
	if (totalWords < wordNumber) return null
	
	for local i:int = 1 to Len(src)
	
		local curChar:string = Mid(src, i, 1)
		if (curChar = Chr(wordSeparater)) then
		
			if (wordCount = wordNumber) then
			
				exit
			
			else 
			
				wordCount = wordCount + 1
				returnWord = null
			
			end if
		
		else
			returnWord = returnWord + curChar; end if
	
	next
	
	if (words(src) = 0 and Len(src)) ..
		returnWord = src
	
	return returnWord

end function

'// -----------------------------------------------------------------------------------------------------------------
'// Description:
'// 		Count the words in a string.
'// 
'// Parameters:
'//		(string) src: The source string.
'// -----------------------------------------------------------------------------------------------------------------
function words:int(src:string, wordSeparater:int = KEY_SPACE)

	local wordCount:int = 1
	
	for local i:int = 1 to Len(src)
	
		local curChar:string = Mid(src, i, 1)
		if (curChar = Chr(wordSeparater)) ..
			wordCount = wordCount + 1
	
	next
	
	if (not Len(src)) ..
		wordCount = 0
	
	return wordCount

end function



'// end of type: str
end type

